import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix
import jieba
import joblib

# 加载停用词表
def load_stopwords(stopwords_file):
    with open(stopwords_file, 'r', encoding='utf-8') as f:
        stopwords = [line.strip() for line in f]
    return stopwords

stopwords = load_stopwords('stopwords.txt')

# 读取数据
data = pd.read_csv('final.csv')

# 将评论文本和情感标签分离
X = data['Review']
y = data['Sentiment']

# 分词并去除停用词
X_segmented = X.apply(lambda x: ' '.join([word for word in jieba.cut(x) if word not in stopwords]))

# 构建 TF-IDF 特征
vectorizer = TfidfVectorizer()
X_vectorized = vectorizer.fit_transform(X_segmented)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_vectorized, y, test_size=0.25, random_state=42)

# 训练朴素贝叶斯模型
clf = MultinomialNB()
clf.fit(X_train, y_train)

# 保存模型
joblib.dump(clf, 'sentiment_classifier_baye.pkl')
joblib.dump(vectorizer, 'tfidf_vectorizer_baye.pkl')

# 使用交叉验证评估模型性能
cv_scores = cross_val_score(clf, X_vectorized, y, cv=5)  # 5折交叉验证

print("交叉验证准确率:", cv_scores.mean())

# 加载保存的模型
clf = joblib.load('sentiment_classifier_baye.pkl')
vectorizer = joblib.load('tfidf_vectorizer_baye.pkl')

print('测试数据：电影真好看，我很喜欢！')
new_text = '电影真好看，我很喜欢！'
new_text_seg = ' '.join([word for word in jieba.cut(new_text) if word not in stopwords])
new_text_feature = vectorizer.transform([new_text_seg])
predicted_label = clf.predict(new_text_feature)[0]
print(f'新文本的情感预测结果为: {predicted_label}')
print('测试数据：什么垃圾电影，浪费我时间！')
new_text = '什么垃圾电影，浪费我时间！'
new_text_seg = ' '.join([word for word in jieba.cut(new_text) if word not in stopwords])
new_text_feature = vectorizer.transform([new_text_seg])
predicted_label = clf.predict(new_text_feature)[0]
print(f'新文本的情感预测结果为: {predicted_label}')
# 使用模型进行预测
new_text = input('请输入评论：')
new_text_seg = ' '.join([word for word in jieba.cut(new_text) if word not in stopwords])
new_text_feature = vectorizer.transform([new_text_seg])
predicted_label = clf.predict(new_text_feature)[0]
print(f'新文本的情感预测结果为: {predicted_label}')